Skip to content

ci: publish to NuGet.org with Trusted Publishing - #21

Merged
BenjaminAbt merged 1 commit into
mainfrom
feature/nuget-trusted-publishing
Nov 21, 2026
Merged

BenjaminAbt merged 1 commit into
mainfrom
feature/nuget-trusted-publishing

Conversation

@BenjaminAbt

Copy link
Copy Markdown
Member

Summary

This PR switches the NuGet.org publishing in release-publish.yml from a stored API key to Trusted Publishing. The workflow exchanges a GitHub OIDC token for a NuGet API key that is valid for one hour, so the repository no longer stores a long-lived NuGet API key that can leak or expire.

The release of 3.2.3 shows why: its publish job failed because the stored key had expired, and 3.2.3 is not on NuGet.org yet.

Forbidden https://www.nuget.org/api/v2/package/
error: Response status code does not indicate success: 403 (The specified API key has expired. Please create a new API key at https://www.nuget.org/account/apikeys.).

The workflow can now also be started manually for an existing release. This is how 3.2.3 will be published after the merge, because a re-run of the failed job would use the old workflow and the expired key again.

Changes

release-publish.yml

  • A new step NuGet login (Trusted Publishing) runs NuGet/login@v1 with user: ${{ secrets.NUGET_USER }}. The push step uses its output steps.nuget-login.outputs.NUGET_API_KEY instead of secrets.NUGET_API_KEY. The login runs right before the push, because the temporary key is valid for one hour.
  • The publish job gets id-token: write, which NuGet/login needs to request the GitHub OIDC token, and contents: read to download the release assets. The workflow-level permissions are reduced to contents: read. The unused packages: write permission is removed; the workflow does not publish to GitHub Packages.
  • New workflow_dispatch trigger with a required tag input. It downloads and publishes the packages of the given GitHub release, for example gh workflow run release-publish.yml -f tag=v3.2.3. --skip-duplicate stays, so versions that are already on NuGet.org are skipped.
  • The release tag is passed to the download script through the RELEASE_TAG environment variable instead of an expression inside the script, so a manually entered tag cannot inject shell code.

README

  • New "Release" section: how a release is built and published, how to publish an existing release again, and which policy and secret the workflow needs.

Setup on NuGet.org and GitHub

These steps need an account with access to the MyCSharp organization on NuGet.org and admin access to this repository.

  1. On NuGet.org, open Trusted Publishing and add a policy for GitHub Actions:

    Field Value
    Policy owner MyCSharp (the organization that owns both packages)
    Repository Owner mycsharp
    Repository HttpClientHints
    Workflow File release-publish.yml (file name only)
    Environment empty
  2. Add the repository secret NUGET_USER with the NuGet.org profile name of the member who created the policy. This is the user name, not the email address and not the organization name.

  3. Merge this PR.

  4. Publish 3.2.3: gh workflow run release-publish.yml -f tag=v3.2.3.

  5. After a successful publish, delete the NUGET_API_KEY repository secret and the expired key on NuGet.org.

Notes for reviewers

  • user for an organization policy: The NuGet docs and the NuGet/login README do not say which account name to use when an organization owns the policy. Other projects with organization-owned packages report that the profile name of the member who created the policy works. If the login step fails with a policy error, check this value first.
  • Policy activation: According to the docs, a new policy can start as temporarily active for 7 days, mainly for private repositories. The first successful publish locks the policy to the repository and owner IDs and makes it permanently active.
  • Environment: The policy and the workflow do not use a GitHub environment. A possible follow-up is an environment such as nuget with protection rules, for example to allow deployments only from main and tags. Its name would then be added to the job and to the policy.
  • Re-runs: Re-running a publish run that started before this change uses the old workflow file and the NUGET_API_KEY secret. Use the manual trigger instead.
  • Testing: PR validation does not run this workflow. The first real run is the manual publish of 3.2.3 after the merge.

Testing

  • The workflow file parses as YAML. It has the triggers release and workflow_dispatch, the job permissions contents: read and id-token: write, and the login step before the push step.
  • The setup and the first publish run will be checked after the merge with the manual run for v3.2.3 and a lookup of the package versions on NuGet.org.

- Exchange the GitHub OIDC token for a temporary NuGet API key with
  NuGet/login instead of using the NUGET_API_KEY secret
- Grant id-token: write to the publish job only and drop the unused
  packages: write permission
- Allow publishing the packages of an existing release manually with
  workflow_dispatch and a tag input
- Pass the release tag to the script through an environment variable
- Describe the release process in the README
@BenjaminAbt
BenjaminAbt merged commit 9d6fbc8 into main Nov 21, 2026
1 check passed
@BenjaminAbt
BenjaminAbt deleted the feature/nuget-trusted-publishing branch November 24, 2026 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant